

import 'package:flutter/material.dart';
import 'package:flutter_again/models/video_info.dart';
import 'package:flutter_again/pages/webview_page.dart';
import 'package:go_router/go_router.dart';
import '../pages/note_page.dart';
import '../pages/setting_page.dart';
import '../pages/surfing_page.dart';
import '../pages/video_page.dart';
import '../pages/video_player_page.dart';
import '../utils/log.dart';
import 'package:flutter_again/utils/json.dart';


final _routingConfig = ValueNotifier(
  RoutingConfig(
    routes: [
      StatefulShellRoute.indexedStack(
        builder: (context, state, navigationShell) {
          return Scaffold(
            bottomNavigationBar: NavigationBar(
              destinations: [
                NavigationDestination(
                  icon: Icon(Icons.note_alt),
                  label: 'Note',
                ),
                NavigationDestination(
                  icon: Icon(Icons.movie),
                  label: 'Video',
                ),
                NavigationDestination(
                  icon: Icon(Icons.surfing),
                  label: 'Surfing',
                ),
                NavigationDestination(
                  icon: Icon(Icons.settings),
                  label: 'Setting',
                ),
              ],
              selectedIndex: navigationShell.currentIndex,
              onDestinationSelected: (int index) => navigationShell.goBranch(index),
            ),
            body: navigationShell,
          );
        },
        branches: [
          StatefulShellBranch(
            initialLocation: '/',
            routes: [
              GoRoute(
                path: '/',
                name: 'tabNote',
                builder: (context, state) {
                  return NotePage();
                },
              ),
            ],
          ),
          StatefulShellBranch(
            initialLocation: '/video',
            routes: [
              GoRoute(
                path: '/video',
                name: 'tabVideo',
                builder: (context, state) {
                  log.d('query: ${state.uri}');
                  return VideoPage();
                },
              ),
            ],
          ),
          StatefulShellBranch(
            initialLocation: '/surfing',
            routes: [
              GoRoute(
                path: '/surfing',
                name: 'tabSurfing',
                builder: (context, state) {
                  return SurfingPage();
                },
              ),
            ],
          ),
          StatefulShellBranch(
            initialLocation: '/setting',
            routes: [
              GoRoute(
                path: '/setting',
                name: 'tabSetting',
                builder: (context, state) {
                  return SettingPage();
                },
              ),
            ],
          ),
        ],
      ),
      GoRoute(
        path: '/video-player',
        name: 'videoPlayer',
        builder: (context, state) {
          var query = state.uri.queryParameters;
          final seqLinksStr = query['seqLinks'] ?? '';
          final numStr = query['num'];
          log.d('numStr: $numStr');
          final num = numStr != null && numStr != '' ? int.parse(numStr) : null;
          List<VideoSeq>? seqLinks;
          if (seqLinksStr.isNotEmpty) {
            seqLinks = jsonParse<List<dynamic>>(seqLinksStr).map((item) => VideoSeq(url: item['url'] ?? '', title: item['title'] ?? '')).toList();
          }
          return VideoPlayerPage(title: query['title'] ?? '', url: query['url'] ?? '', platformId: query['platformId'] ?? '', seqLinks: seqLinks, num: num);
        },
      ),
      GoRoute(
        path: '/webview',
        name: 'webview',
        builder: (context, state) {
          var query = state.uri.queryParameters;
          return WebViewPage(url: query['url'] ?? '');
        },
      ),
    ],
  ),
);

final router = GoRouter.routingConfig(routingConfig: _routingConfig);

void addRoute(List<RouteBase> routes) {
  _routingConfig.value = RoutingConfig(
    routes: [
      ..._routingConfig.value.routes,
      ...routes,
    ],
    redirectLimit: _routingConfig.value.redirectLimit,
    redirect: _routingConfig.value.redirect,
  );
}
